﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SQLH2ConfigTool
{
    public partial class frmMain : Form
    {

        ConfigurationFile cf;

        public frmMain()
        {
            InitializeComponent();
            DisableScreen();            
        }

        public void enableScreen()
        {
            pnlServers.Enabled = true;
            grpSettings.Enabled = true;
        }

        public void DisableScreen()
        {
            pnlServers.Enabled = false;
            grpSettings.Enabled = false;
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog od = new OpenFileDialog();
            if (od.ShowDialog() == DialogResult.OK && od.FileName != null)
            {
                txtConfigFile.Text = od.FileName;
                LoadConfigFile(od.FileName);
                enableScreen();
            }
        }

        private void LoadConfigFile(string FileName)
        {
            cf = new ConfigurationFile(FileName);
            refreshScreen();
        }

        private void refreshScreen()
        {
            lstvCounters.Items.Clear();
            lstServers.Items.Clear();

            txtCollectionInterval.Text = cf.CollectionIntervalSeconds.ToString();
            txtMaxFileSize.Text = cf.MaxFileSizeMB.ToString();
            txtRetryInterval.Text = cf.RetryIntervalSeconds.ToString();
            chkVerboseLogging.Checked = cf.VerboseLogging;

            this.Text += " : " + cf.Version;

            foreach (Server s in cf.Servers)
            {
                lstServers.Items.Add(s);
            }
        }

        private void lstServers_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshCounterList();
        }

        private void refreshCounterList()
        {
            Server s = (Server)lstServers.SelectedItem;

            lstvCounters.Items.Clear();

            ListViewGroup grp;
            
            foreach (Counter c in s.Counters)
            {
                //create group if it does not exist

                grp = lstvCounters.Groups[c.Object];
                if (grp == null)
                {
                    grp = new ListViewGroup(c.Object, c.Object);
                    lstvCounters.Groups.Add(grp);
                }

                ListViewItem li = new ListViewItem();
                li.Text = c.Object;
                li.Tag = c;

                li.Group = grp;
                
                ListViewItem.ListViewSubItem sliObject = new ListViewItem.ListViewSubItem();
                sliObject.Text = c.CounterName;

                ListViewItem.ListViewSubItem sliInstance = new ListViewItem.ListViewSubItem();
                sliInstance.Text = c.Instance;

                li.SubItems.Add(sliObject);
                li.SubItems.Add(sliInstance);
                
                lstvCounters.Items.Add(li);
            }
        }

        private void btnAddCounter_Click(object sender, EventArgs e)
        {
            Server SelectedServer = (Server)lstServers.SelectedItem;
            if (lstServers.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a server"); return;
            }

            frmAddCounter fCounter = new frmAddCounter(SelectedServer);
            if (fCounter.ShowDialog() == DialogResult.OK)
            {
                SelectedServer.Counters.Add(fCounter.SelectedCounter);
                refreshCounterList();
            }
        }

        private void btnDeleteCounter_Click(object sender, EventArgs e)
        {
            Server SelectedServer = (Server)lstServers.SelectedItem;
            int i=0;
            for (i = 0; i < SelectedServer.Counters.Count; i++ )
            {
                Counter ForDelete = SelectedServer.Counters[i];
                if (ForDelete.Equals((Counter)lstvCounters.SelectedItems[0].Tag))
                {
                    SelectedServer.Counters.RemoveAt(i);
                }
            }
            refreshCounterList();
        }

        private void btnAddServer_Click(object sender, EventArgs e)
        {
            frmAddServer c = new frmAddServer(cf);
            if (c.ShowDialog() == DialogResult.OK)
            {
                Server s = new Server();
                s.ServerName = c.ServerName;
                cf.Servers.Add(s);

                refreshScreen();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            cf.Save();
            MessageBox.Show("Configuration Saved");
        }

        private void chkVerboseLogging_CheckedChanged(object sender, EventArgs e)
        {
            cf.VerboseLogging = chkVerboseLogging.Checked;
        }

        private void txtCollectionInterval_TextChanged(object sender, EventArgs e)
        {
            cf.CollectionIntervalSeconds = Convert.ToInt32(txtCollectionInterval.Text);
        }

        private void txtRetryInterval_TextChanged(object sender, EventArgs e)
        {
            cf.RetryIntervalSeconds = Convert.ToInt32(txtRetryInterval.Text);
        }

        private void txtMaxFileSize_TextChanged(object sender, EventArgs e)
        {
            cf.MaxFileSizeMB = Convert.ToInt32(txtMaxFileSize.Text);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About a = new About();
            a.ShowDialog();
        }
    }
}
